//
//  reportViewController.m
//  GaKan
//
//  Created by hztech on 2017/12/27.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "reportViewController.h"
#import "JTCalendar.h"
#import "textViewController.h"
#import "WXDUse.h"

@interface reportViewController ()<JTCalendarDelegate>
//日期条
@property (strong, nonatomic)  JTCalendarMenuView *calendarMenuView;
//日历界面
@property (strong, nonatomic)  JTHorizontalCalendarView *calendarContentView;

@property (strong, nonatomic) JTCalendarManager *calendarManager;

@end

@implementation reportViewController
//全局变量
{
    NSMutableDictionary *_eventsByDate;
    NSMutableDictionary *_ByDate;
    
    NSMutableArray *_datesSelected;
    BOOL _selectionMode;
    
    NSMutableArray *dailyData;
}
//重写init
-(instancetype)init{
    self = [super init];
    if (self)
    {
        [self setToptittle:@"日志登记"];
        [self setTopTittleColor:[UIColor whiteColor]];
        [self setTopBgColor:headColor];
        [self setBackBtnImage:[UIImage imageNamed:@"wo_merchandise_detail_back@3x"]];
        [self setBackBtnHide:NO];
    }
    return self;
}
//主函数--搭建UI界面
- (void)viewDidLoad {
    self.calendarMenuView = [[JTCalendarMenuView alloc]initWithFrame:CGRectMake(0, 60, ScreenWidth, 30)];
    self.calendarContentView = [[JTHorizontalCalendarView alloc]initWithFrame:CGRectMake(0, 90, ScreenWidth, 300)];
    
    [self.view addSubview:_calendarMenuView];
    [self.view addSubview:_calendarContentView];
    
    _calendarManager = [JTCalendarManager new];
    _calendarManager.delegate = self;
    
    // Generate random events sort by date using a dateformatter for the demonstration
    //[self createRandomEvents];
    _datesSelected=[NSMutableArray new];
    dailyData = [NSMutableArray new];
    [self createRandomSelect];
    
    [_calendarManager setMenuView:_calendarMenuView];
    [_calendarManager setContentView:_calendarContentView];
    [_calendarManager setDate:[NSDate date]];
    
    _datesSelected = [NSMutableArray new];
    _selectionMode = NO;
    
}
#pragma mark - CalendarManager delegate
//日历控件协议代理

- (void)calendar:(JTCalendarManager *)calendar prepareDayView:(JTCalendarDayView *)dayView
{
    dayView.hidden = NO;
    // Hide if from another month
    if([dayView isFromAnotherMonth]){
        dayView.hidden = YES;
    }
    
    // Selected date
    else if([self isInDatesSelected:dayView.date]){
        dayView.circleView.hidden = NO;
        dayView.circleView.backgroundColor = xdGrayColor;
        //dayView.dotView.backgroundColor = [UIColor blackColor];
        dayView.textLabel.textColor = [UIColor blackColor];
    }
    // Other month
    else if(![_calendarManager.dateHelper date:_calendarContentView.date isTheSameMonthThan:dayView.date]){
        dayView.circleView.hidden = YES;
        dayView.dotView.backgroundColor = [UIColor redColor];
        dayView.textLabel.textColor = [UIColor lightGrayColor];
    }
    // Another day of the current month
    else{
        dayView.circleView.hidden = YES;
        dayView.dotView.backgroundColor = [UIColor redColor];
        dayView.textLabel.textColor = [UIColor blackColor];
    }
    
}

- (void)calendar:(JTCalendarManager *)calendar didTouchDayView:(JTCalendarDayView *)dayView
{
    textViewController *view=[textViewController new];
    view.today=dayView.date;
    [view takeDate];
    view.confirmBlock = ^(NSDate *daily_date) {
        [self createRandomSelect];
    };
    if([self isInDatesSelected:dayView.date]){
        for (daily*obj in dailyData) {
            if ([obj.daily_date isEqual:dayView.date]) {
                view.hxt = obj;
                NSLog(@"%@",obj);
            }
        }
        [view takeDaily];
    }
    [[ViewManager shareInstance].NavigationController pushViewController:view animated:YES];
}
- (void)calendar:(JTCalendarManager *)calendar prepareMenuItemView:(UILabel *)menuItemView date:(NSDate *)date
{
    static NSDateFormatter *dateFormatter;
    if(!dateFormatter){
        dateFormatter = [NSDateFormatter new];
        dateFormatter.dateFormat = @"yyyy年MMMM";
        
        dateFormatter.locale = _calendarManager.dateHelper.calendar.locale;
        dateFormatter.timeZone = _calendarManager.dateHelper.calendar.timeZone;
    }
    
    menuItemView.text = [dateFormatter stringFromDate:date];
}
#pragma mark - Date selection
//判断是否有历史记录
- (BOOL)isInDatesSelected:(NSDate *)date
{
    for(NSDate *dateSelected in _datesSelected){
        //        if([_calendarManager.dateHelper date:dateSelected isTheSameDayThan:date]){
        //            return YES;
        //        }
        if ([dateSelected isEqualToDate:date]) {
            return YES;
        }
    }
    
    return NO;
}


//获取shu
- (void)createRandomSelect
{
    [_datesSelected removeAllObjects];
    [dailyData removeAllObjects];
    
    BmobQuery   *bquery = [BmobQuery queryWithClassName:@"daily"];
    
    [bquery whereKey:@"user_id" equalTo:[[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"]];
    NSLog(@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"]);
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        NSLog(@"%@",error);
        for (BmobObject *obj in array) {
            
            NSDate *randomDate = [obj objectForKey:@"daily_date"];
            NSLog(@"%@",randomDate);
            [_datesSelected addObject:randomDate];
            
            daily *hxt = [daily new];
            hxt.daily_id = [obj objectId];
            hxt.daily_date = [obj objectForKey:@"daily_date"];
            hxt.daily_project = [obj objectForKey:@"daily_project"];
            hxt.daily_string = [obj objectForKey:@"daily_string"];
            hxt.daily_time = [obj objectForKey:@"daily_time"];
            [dailyData addObject:hxt];
        }
        [self removeLoading];
        [_calendarManager reload];
    }];
}
#pragma mark - Fake data

// Used only to have a key for _eventsByDate
- (NSDateFormatter *)dateFormatter
{
    static NSDateFormatter *dateFormatter;
    if(!dateFormatter){
        dateFormatter = [NSDateFormatter new];
        dateFormatter.dateFormat = @"dd-MM-yyyy";
    }
    
    return dateFormatter;
}

- (BOOL)haveEventForDay:(NSDate *)date
{
    NSString *key = [[self dateFormatter] stringFromDate:date];
    
    if(_eventsByDate[key] && [(NSArray*)_eventsByDate[key] count] > 0){
        return YES;
    }
    
    return NO;
    
}


@end
